<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="MD5(MD5(pass明文+固定salt)+salt)" prop="password">
      <el-input v-model="dataForm.password" placeholder="MD5(MD5(pass明文+固定salt)+salt)"></el-input>
    </el-form-item>
    <el-form-item label="盐" prop="salt">
      <el-input v-model="dataForm.salt" placeholder="盐"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="head">
      <el-input v-model="dataForm.head" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="registerData">
      <el-input v-model="dataForm.registerData" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="最后登录时间" prop="lastLoginData">
      <el-input v-model="dataForm.lastLoginData" placeholder="最后登录时间"></el-input>
    </el-form-item>
    <el-form-item label="登录次数" prop="loginCount">
      <el-input v-model="dataForm.loginCount" placeholder="登录次数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          nickname: '',
          password: '',
          salt: '',
          head: '',
          registerData: '',
          lastLoginData: '',
          loginCount: ''
        },
        dataRule: {
          nickname: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: 'MD5(MD5(pass明文+固定salt)+salt)不能为空', trigger: 'blur' }
          ],
          salt: [
            { required: true, message: '盐不能为空', trigger: 'blur' }
          ],
          head: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          registerData: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          lastLoginData: [
            { required: true, message: '最后登录时间不能为空', trigger: 'blur' }
          ],
          loginCount: [
            { required: true, message: '登录次数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.nickname = data.user.nickname
                this.dataForm.password = data.user.password
                this.dataForm.salt = data.user.salt
                this.dataForm.head = data.user.head
                this.dataForm.registerData = data.user.registerData
                this.dataForm.lastLoginData = data.user.lastLoginData
                this.dataForm.loginCount = data.user.loginCount
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'nickname': this.dataForm.nickname,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'head': this.dataForm.head,
                'registerData': this.dataForm.registerData,
                'lastLoginData': this.dataForm.lastLoginData,
                'loginCount': this.dataForm.loginCount
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
